package org.example.mapper;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Select;
import org.example.entity.PointRecords;
import org.example.dto.UserPointUsageDTO;

import java.util.List;

@Mapper
public interface PointRecordsMapper extends BaseMapper<PointRecords> {
    
    // 根据用户ID查询积分记录
    @Select("SELECT * FROM point_records WHERE user_id = #{userId} ORDER BY created_time DESC")
    List<PointRecords> getPointRecordsByUserId(Long userId);
    
    // 根据用户ID统计积分使用情况
    @Select("SELECT " +
            "SUM(CASE WHEN type = 1 THEN points ELSE 0 END) as totalEarned, " +
            "SUM(CASE WHEN type = 2 THEN ABS(points) ELSE 0 END) as totalSpent, " +
            "SUM(CASE WHEN type = 3 THEN points ELSE 0 END) as totalBonus " +
            "FROM point_records WHERE user_id = #{userId}")
    UserPointUsageDTO getPointUsageStats(Long userId);
} 